package com.qyhl.school.school.reporter.sign;

import com.qyhl.school.common.SchoolUrl;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.PathConfigConstant;
import com.qyhl.webtv.commonlib.entity.UpTokenBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

public class SchoolReportSignModel implements SchoolReportSignContract.SchoolReportSignModel {
    private SchoolReportSignPresenter mPresenter;

    public SchoolReportSignModel(SchoolReportSignPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void doSign(String username, String realName, int sex, String phoneNum, int age, int schoolId, String schoolName, String logo) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(SchoolUrl.BASE_SCHOOL_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.SCHOOL_VERSION);
            object.put("name", "signUpReporter");
            object.put("username", username);
            object.put("realName", realName);
            object.put("phoneNum", phoneNum);
            object.put("sex", sex);
            object.put("age", age);
            object.put("schoolId", schoolId);
            object.put("schoolName", schoolName);
            object.put("logo", logo);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.signResult(false, e.getMessage());
                    }

                    @Override
                    public void onSuccess(String bean) {
                        mPresenter.signResult(true, "申请成功！");
                    }
                });
    }

    @Override
    public void getToken(boolean isUpload) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(SchoolUrl.BASE_SCHOOL_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.SCHOOL_VERSION);
            object.put("name", "getToken");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<UpTokenBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getTokenError(isUpload);
                    }

                    @Override
                    public void onSuccess(UpTokenBean bean) {
                        mPresenter.getTokenSuccess(bean, isUpload);
                    }
                });
    }
}
